package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.R;

import java.util.HashMap;
import java.util.List;

public class VideoSubAdapter extends RecyclerView.Adapter<VideoSubAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<String> mData;
    private OnItemClickListener mOnItemClickListener = null;
    private HashMap<Integer, Boolean> hashMap = new HashMap<>();

    public VideoSubAdapter(Context context, List<String> data, int position) {
        this.context = context;
        this.mData = data;
        if (mData != null) {
            if (mData.size() > 0) {
                for (int i = 0; i < mData.size(); i++) {
                    if (i == position) {
                        hashMap.put(i, true);
                    } else {
                        hashMap.put(i, false);
                    }
                }
            }
        }
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_sub, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                boolean isChecked = hashMap.get(position);
                holder.tvText.setText(mData.get(position) + "");
                if (isChecked) {
                    holder.tvPlaying.setVisibility(View.VISIBLE);
                    holder.tvText.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                } else {
                    holder.tvPlaying.setVisibility(View.GONE);
                    holder.tvText.setTextColor(context.getResources().getColor(R.color.text_color333));
                }
            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void upData(int position) {
        for (int i = 0; i < mData.size(); i++) {
            if (i == position) {
                hashMap.put(i, true);
            } else {
                hashMap.put(i, false);
            }
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
            for (int i = 0; i < mData.size(); i++) {
                hashMap.put(i, false);
            }
            hashMap.put((int) v.getTag(), true);
            notifyDataSetChanged();
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View view;
        private TextView tvText;
        private TextView tvPlaying;

        public ActivityHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvText = (TextView) view.findViewById(R.id.tv_text);
            tvPlaying = (TextView) view.findViewById(R.id.tv_playing);

        }
    }

}
